import React from "react";
import { Menu } from "antd";
import {
  LaptopOutlined,
  NotificationOutlined,
  UserOutlined,
} from "@ant-design/icons";
import { adminRoutes } from "@/router";
import { withRouter } from "react-router-dom";
import {connect} from 'react-redux'
const { SubMenu } = Menu;
// let filterRoutes = ()=>{
//   let arr = []
//   // let routes = [...adminRoutes]
//   adminRoutes.forEach(item=>{
//     if(!item.menuhide){
//       if(item.children){
//         item.children.forEach((itm,idx)=>{
//           if(itm.menuhide){
//             // console.log(itm.label);
//             item.children.splice(idx,1)
//           }
//         })
//       }
//       arr.push(item)
//     }
//   })
//   return arr
// }
function SideMenu(props) {
  let handleMenu = ({ key }) => {
    console.log(key, props);
    props.history.push(key);
  };
  // console.log('侧边菜单',props);
  let {rolecode} = props.user.userInfo //获取当前登录用户的角色代号
  // console.log(filterRoutes());
  return (
    // <Menu
    //   mode="inline"
    //   defaultSelectedKeys={['1']}
    //   defaultOpenKeys={['sub1']}
    //   style={{
    //     height: '100%',
    //     borderRight: 0,
    //   }}
    //   items={adminRoutes}
    //   onClick={handleMenu}
    // />
    <Menu
      onClick={handleMenu}
      defaultSelectedKeys={["/admin/dashbord"]}
      mode="inline"
      theme="dark"
    >
      {adminRoutes.map((item) => {
        
        let bool = item.roles.includes(rolecode)
        if(!bool){return ''} //根据权限控制主菜单的渲染
        if (item.children) {
          return (
            <SubMenu
              key={item.key}
              title={item.label}
              icon={item.icon}
            >
              {
                item.children.map(itm=>{
                  let bool = itm.roles.includes(rolecode)
                  return !itm.hide&&bool?<Menu.Item key={itm.key}>{itm.label}</Menu.Item>:''
                })
              }
            </SubMenu>
          );
        }
        return <Menu.Item key={item.key} icon={item.icon}>{item.label}</Menu.Item>;
      })}
    </Menu>
  );
}

export default connect(({user})=>({user}))(withRouter(SideMenu));
